home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 178_01 / tvx_unix.c < prev   
C/C++ Source or Header  |  1986-01-16  |  14KB  |  583 lines

  1. /* -------------------------- tvx_unix.c ------------------------------ */
  2. #include "tvx_defs.ic"
  3. #include "tvx_glbl.ic"
  4.  
  5. #define TEMPEXT ".$$1"        /* temporary file */
  6. #define BACKEXT ".B"        /* backup file */
  7. #define SWITCH '-'
  8. #define FILESEP '.'
  9.  
  10. /* define USETMP if you want intermediate workfiles built on
  11.    /tmp.  Otherwise, they will be built on the same directory as
  12.    the original file.  This latter method is often a bit faster,
  13.    especially when exiting if /tmp is on a different volume than
  14.    the destination file (which makes mv COPY the file rather than
  15.    just renameing. */
  16.  
  17. /* #define USETMP */            /* define if create temp files on /tmp */
  18.  
  19. #include <ctype.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22.  
  23.  
  24. /* --------------  terminal I/O stuff --------------- */
  25.  
  26. static struct sgttyb sgb;
  27. static struct tchars tch;
  28. static struct ltchars ltc;
  29.  
  30. #define Ioctl ioctl
  31. #define Read read
  32. #define Write write
  33.  
  34. /* ------------- file mode stuff ---------------------- */
  35. #include <sys/stat.h>
  36.   static struct stat info;        /* structure to get info */
  37.  
  38. /* ------------- misc stuff ---------------------- */
  39.  
  40.   extern int errno;
  41.   extern char **environ;
  42.  
  43.  
  44. #ifdef TERMCAP            /* routines needed for termcap */
  45. /* ------------- termcap stuff ---------------------- */
  46.   char PC;
  47.   char *BC;
  48.   char *UP;
  49.   char TERM[40];
  50.   short ospeed;
  51.  
  52.   static char Tcm[80];        /* special entry for cm */
  53.   static char empty[2];
  54.   static char Tbc[20];
  55.   static char Tup[20];
  56.  
  57.   static int    Tco,            /* number of columns per line */
  58.         Tli;            /* number of lines */
  59.  
  60.   static char tcbuff[1024];        /* buffer to hold termcap entry */
  61.  
  62.  
  63. /* ==========================>>> gettermcap  <<<========================= */
  64.   gettermcap()
  65.   {
  66.     char *tp;
  67.     char *getenv();
  68.     char entry[80];        /* scratch buffer for entry */
  69.  
  70.     empty[0] = 0;
  71.  
  72.     ospeed = sgb.sg_ospeed;    /* get the speed */
  73.  
  74.     if ((tp = getenv("TERM")) == NULL)
  75.       {
  76.     goto FORCETTY;
  77.       }
  78.     strcpy(TERM,tp);        /* copy to our TERM */
  79.  
  80.     if (tgetent(tcbuff,TERM) < 1)
  81.       {
  82.     goto FORCETTY;
  83.       }
  84.  
  85. /*    read required termcap entries, save in appropriate TVX arrays */
  86.  
  87.     if (!gettcap("cm",Tcm))
  88.       {
  89.     goto FORCETTY;
  90.       }
  91.  
  92.     if (!gettcap("ce",entry))
  93.       {
  94.     goto FORCETTY;
  95.       }
  96.     if (!capcpy(celin,entry,7))    /* copy entry to end of line */
  97.       {
  98.     goto FORCETTY;
  99.       }
  100.     
  101.     gettcap("cd",entry);        /* clear to end of display */
  102.     capcpy(cescr,entry,7);
  103.  
  104.     gettcap("al",entry);        /* insert a line (add line) */
  105.     capcpy(ciline,entry,7);
  106.  
  107.     gettcap("dl",entry);    /* delete a line */
  108.     capcpy(ckline,entry,7);
  109.  
  110.     if (!gettcap("sr",entry))    /* reverse scroll */
  111.       {
  112.     strcpy(ctopb,ciline);    /* add line works the same */
  113.       }
  114.     else
  115.     capcpy(ctopb,entry,7);
  116.  
  117.     gettcap("ve",entry);        /* stand cursor changer end */
  118.     capcpy(ccsrcm,entry,7);
  119.     gettcap("vs",entry);        /* stand cursor changer begin */
  120.     capcpy(ccsrin,entry,7);
  121.  
  122.     gettcap("se",entry);        /* stand out end */
  123.     capcpy(cbolde,entry,7);
  124.  
  125.     gettcap("so",entry);        /* begin standout */
  126.     capcpy(cboldb,entry,7);
  127.  
  128.     cerred[0] = 7;                /* bell for sure */
  129.     gettcap("vb",entry);        /* visual bell? */
  130.     if (*entry)
  131.     capcpy(cerred,entry,7);
  132.  
  133.     if (!capcpy(&cversn[1],TERM,10))        /* copy name to version */
  134.     strcpy(cversn,"TERMCAP");
  135.  
  136.     if ((Tco = tgetnum("co")) < 0)    /* # of cols */
  137.     Tco = 79;        /* assume 80 x 24 */
  138.     if ((Tli = tgetnum("li")) < 0)    /* # of lines */
  139.     Tli = 24;        /* assume 80 x 24 */
  140.  
  141.     tvhardlines = tvlins = Tli;    /* number of lines */
  142.     tvcols = Tco - 1; /* set col val (-1 avoids all the line wrap crap )*/
  143.     if (tvhardlines != 24 || tvhardlines != 25)    /* strange terminal */
  144.       {
  145.     ddline = (tvlins / 2) + 1;
  146.     setdscrl();        /* calculate scroll */
  147.       }
  148.  
  149.     gettcap("bc",entry);        /* get backspace character */
  150.     if (!*entry)
  151.       {
  152.     Tbc[0] = 8; Tbc[1] = 0;
  153.       }
  154.     else
  155.     capcpy(Tbc,entry,19);
  156.     BC = Tbc;
  157.     gettcap("up",entry);        /* get backspace character */
  158.     if (!*entry)
  159.       {
  160.     Tup[0] = 0;
  161.       }
  162.     else
  163.     capcpy(Tup,entry,19);
  164.     UP = Tup;
  165.     gettcap("pc",entry);        /* get the pad character */
  166.     PC = *entry;
  167.  
  168.     gettcap("is",entry);        /* initialization string */
  169.     tcapcs(entry);            /* send the intialization string */
  170.  
  171.     gettcap("ti",entry);        /* cm initialization string */
  172.     tcapcs(entry);            /* send the intialization string */
  173.  
  174.     return;
  175.  
  176. FORCETTY:
  177.    force_tty = TRUE;
  178.    remark("Unable to set up for video terminal, tty mode assumed.");
  179.    strcpy(cversn,"tty");
  180.   }
  181.  
  182. /* =============================>>> capcpy  <<<============================= */
  183.   capcpy(to,from,len)
  184.   char *to, *from;
  185.   int len;
  186.   {        /* copy a capability, checking length */
  187.     if (strlen(from) > len)
  188.       {
  189.     *to = 0;
  190.     return (FALSE);
  191.       }
  192.     else
  193.     strcpy(to,from);
  194.     return (TRUE);
  195.   }
  196.  
  197. /* =============================>>> gettcap  <<<============================= */
  198.   gettcap(cap,area)
  199.   char *cap, *area;
  200.   {
  201.     char **cpp, *cp;
  202.  
  203.     cpp = &cp;        /* I think */
  204.     cp = area;
  205.     *area = 0;        /* assume null entry */
  206.  
  207.     tgetstr(cap,cpp);    /* get the capability */
  208.     return (*area);        /* return 1st char */
  209.     
  210.   }
  211.  
  212. /* =============================>>> tcapcs  <<<============================= */
  213.   tcapcs(str)
  214.   char *str;
  215.   {
  216.      /* send a termcap generated control string to terminal */
  217.  
  218.     register char *cp;
  219.     int ttwt();
  220.  
  221.     if (!(echof && !bakflg && !ttymode))
  222.     return;
  223.     if (!*str)        /* don't send null strings */
  224.     return;
  225.     cp = str;
  226.     tputs(cp,1,ttwt);
  227.  
  228.   }
  229.  
  230. /* =============================>>> tcapxy  <<<============================= */
  231.   tcapxy(x,y)
  232.   int x,y;
  233.   {
  234.     /* move cursor to x,y */
  235.  
  236.    char *tgoto();
  237.  
  238.    tcapcs(tgoto(Tcm,x-1,y-1));    /* send the string, adjusting x,y */
  239.  
  240.   }
  241. #endif   /* termcap */
  242.  
  243.  
  244. /* =============================>>> ttinit  <<<============================= */
  245.   ttinit()
  246.   {
  247.     struct sgttyb nsgb;
  248.     struct tchars ntch;
  249.     struct ltchars nltc;
  250.  
  251.     (void) Ioctl(0, TIOCGETP, &sgb);
  252.     (void) Ioctl(0, TIOCGETP, &nsgb);
  253.     (void) Ioctl(0, TIOCGETC, &tch);
  254.     (void) Ioctl(0, TIOCGETC, &ntch);
  255.     (void) Ioctl(0, TIOCGLTC, <c);
  256.  
  257.     nsgb.sg_flags |= CBREAK;
  258.     nsgb.sg_flags &= ~(CRMOD|ECHO|LCASE|TANDEM);
  259.  
  260.     ntch.t_intrc = -1;  /* interrupt */
  261.     ntch.t_quitc = -1;  /* quit */
  262.  
  263. /* the following two lines control flow control */
  264.  
  265. #ifndef FLOWCONTROL
  266.     ntch.t_startc = -1; /* start output */
  267.     ntch.t_stopc = -1;  /* stop output */
  268. #endif
  269.  
  270.     ntch.t_eofc = -1;   /* end-of-file */
  271.     ntch.t_brkc = -1;   /* input delimiter (like nl) */
  272.  
  273.     nltc.t_suspc = -1;  /* stop process signal */
  274.     nltc.t_dsuspc = -1; /* delayed stop process signal */
  275.     nltc.t_rprntc = -1; /* reprint line */
  276.     nltc.t_flushc = -1; /* flush output (toggles) */
  277.     nltc.t_werasc = -1; /* word erase */
  278.     nltc.t_lnextc = -1; /* literal next character */
  279.  
  280.     (void) Ioctl(0, TIOCSETP, &nsgb);
  281.     (void) Ioctl(0, TIOCSETC, &ntch);
  282.     (void) Ioctl(0, TIOCSLTC, &nltc);
  283.  
  284. #ifdef TERMCAP
  285.     gettermcap();            /* set up terminal characteristics */
  286. #endif
  287.  
  288.     info.st_mode = -1;            /* no mode stuff yet */
  289.  }
  290.  
  291.  
  292. /* =============================>>> ttrd_unix  <<<============================= */
  293.   ttrd_unix()
  294.   {
  295.            char c;
  296.  
  297.     Read(0, &c, 1);
  298.     return(c);
  299.   }
  300.  
  301. /* =============================>>> ttwtln  <<<============================= */
  302.   ttwtln(cbuf,cnt)
  303.   char *cbuf;
  304.   int cnt;
  305.   {
  306.     if (echof && !bakflg && !ttymode)
  307.     Write(1, cbuf, cnt);
  308.   }
  309.  
  310. /* =============================>>> ttwt  <<<============================= */
  311.   ttwt(c)
  312.   char c;
  313.   {
  314.     if (ttymode)
  315.     return;
  316.     Write(1, &c, 1);
  317.   }
  318.  
  319. /* =============================>>> ttclos  <<<============================= */
  320.   ttclos()
  321.   {
  322.  
  323. #ifdef TERMCAP
  324.     char entry[80];
  325.  
  326.     gettcap("te",entry);        /* cm end up string */
  327.     tcapcs(entry);            /* send it */
  328.  
  329. #endif
  330.     (void) Ioctl(0, TIOCSETP, &sgb);
  331.     (void) Ioctl(0, TIOCSETC, &tch);
  332.